package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import com.itheima.health.util.POIUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件上传controller
 *
 * @author hongBoZ
 * @since 2022/11/22 18:12
 */
@Slf4j
@RestController
@RequestMapping("ordersetting")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    /**
     * 文件上传
     *
     * @param excelFile
     * @return
     */
    @PostMapping("upload")
    public Result upload(@RequestBody MultipartFile excelFile) {
        log.info("文件上传前后端交互成功");
        log.info("excelFile ===> {}", excelFile);
        try {
            this.ordersettingService.upload(excelFile);
            return new Result(true, MessageConst.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            if (!(e.getMessage().equals(MessageConst.IMPORT_ORDERSETTING_FAIL))) {
                throw new BusinessRuntimeException(e.getMessage());
            }
            return new Result(false, MessageConst.IMPORT_ORDERSETTING_FAIL);
        }
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(Integer year,Integer month){
        log.info("aaa");

        if (month>0&&month<10){
            month = Integer.parseInt("0"+month);
        }
        String moth = year + "-" + month;
        List<OrderSetting> list = ordersettingService.getOrderSettingByMonth(moth);

        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS,data);
    }

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
            ordersettingService.editNumberByDate(orderSetting);
            //预约设置成功
            return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }
}
